package com.sunwayworld.basemodule.common.errcode.resource.impl;

import com.alibaba.fastjson.JSONObject;
import com.sunwayworld.basemodule.common.errcode.ErrCodeManager;
import com.sunwayworld.basemodule.common.errcode.resource.ErrCodeResource;
import com.sunwayworld.framework.data.page.Page;
import com.sunwayworld.framework.data.page.Pagination;
import com.sunwayworld.framework.log.annotation.LogModule;
import com.sunwayworld.framework.restful.data.RestJsonWrapperBean;
import com.sunwayworld.framework.spring.annotation.GikamBean;
import com.sunwayworld.framework.utils.ObjectUtils;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@LogModule("错误码")
@RestController
@GikamBean
public class ErrCodeResourceImpl implements ErrCodeResource {
    @Override
    public Page<Map<String,String>> selectPagination(RestJsonWrapperBean wrapperBean) {
        Map<String, String> paramMap = wrapperBean.getParamMap();
        String qf = paramMap.get("qf");
        String code_cisc = null,desc_cisc = null;
        if(!ObjectUtils.isEmpty(qf)){
            JSONObject jsonObject = JSONObject.parseObject(qf);
            code_cisc = jsonObject.getString("code_CISC");
            desc_cisc = jsonObject.getString("desc_CISC");
        }
        Map<String, String> globalCodes = ErrCodeManager.globalCodes;
        List<Map<String,String>> list = new ArrayList<>();
        String finalCode_cisc = code_cisc;
        String finalDesc_cisc = desc_cisc;
        globalCodes.forEach((k, v)->{
            Map<String, String> e = new HashMap<>();
            if(!ObjectUtils.isEmpty(finalCode_cisc) && !k.contains(finalCode_cisc)){
                return;
            }
            if(!ObjectUtils.isEmpty(finalDesc_cisc) && !v.contains(finalDesc_cisc)){
                return;
            }
            e.put("code",k);
            e.put("desc",v);
            list.add(e);
        });
        return new Pagination<>(list);
    }
}
